package gui;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;

import types.StreamDescriptor;

@ManagedBean(name = "streamList")
public class StreamList implements Serializable {

	private static final long serialVersionUID = -3107833603269523850L;
	private static final String TWITTER = "twitter";
	private static final String YOUTUBE = "youtube";
	private GetData data = new GetData();
	public StreamDescriptor stream;
	public List<StreamDescriptor> streams; 
	private String id;
	private String selectedOption = YOUTUBE;	
	private List<String> selectedOptions= new ArrayList<String>(){{
	    add(TWITTER);
	    add(YOUTUBE);
	}};

	public StreamList(){
		createList();
	}
	
	/**
	 * Create the default list of all enties in the db
	 */
	public void createList() {
		this.streams = new ArrayList<StreamDescriptor>();

		List<StreamDescriptor> realstreams = data.getStreams();
		for (StreamDescriptor streamDescriptor : realstreams) {
			this.streams.add(streamDescriptor);
		}
	}

	/**
	 * Update the list to the selected options
	 * 
	 * @param ae ActionEvent, that triggers this method
	 */
	public void updateList(ActionEvent ae) {
		this.streams = null;
		this.streams = new ArrayList<StreamDescriptor>();
		
		for (String selectedOp : this.selectedOptions) {
			if(selectedOp.equals(YOUTUBE)){
				this.streams.addAll(data.getYoutubeStreams());
			}else if(selectedOp.equals(TWITTER)){
				this.streams.addAll(data.getTwitterStreams());
			}
				
		}
	}

	/**
	 * Getting the selected stream id
	 * 
	 * @return the id of the selected stream
	 */
	public String getId() {
		return id;
	}

	/**
	 * Save the id of the selected stream
	 * 
	 * @param id of the selected stream
	 */
	public void setId(String id) {
		this.id = id;
	}
	
	/**
	 * Getting the list of all streams
	 * 
	 * @return list of all streams
	 */
	public List<StreamDescriptor> getStreams() {
		return streams;
	}

	/**
	 * Setting the list of all streams
	 * 
	 * @param streams list of streamdescriptor
	 */
	public void setStreams(List<StreamDescriptor> streams) {
		this.streams = streams;
	}	
	
	/**
	 * Getter for the selected options
	 * 
	 * @return the list of all selected options
	 */
	public List<String> getSelectedOptions() {
		return selectedOptions;
	}

	/**
	 * Setter for the selected options
	 * 
	 * @param selectedOptions list of options
	 */
	public void setSelectedOptions(List<String> selectedOptions) {
		this.selectedOptions = selectedOptions;
	}

	/**
	 * Getter for the select options. Used to select one options.
	 * 
	 * @return selected option
	 */
	public String getSelectedOption() {
		return selectedOption;
	}

	/**
	 * Setter for the selected option. Used to select one options.
	 * 
	 * @param selectedOption option
	 */
	public void setSelectedOption(String selectedOption) {
		this.selectedOption = selectedOption;
	}
	
	/**
	 * Twitter constant
	 * 
	 * @return the constant for Twitter string
	 */
	public static String getTwitter() {
		return TWITTER;
	}

	/**
	 * YouTube constant
	 * 
	 * @return the constant for YouTube string
	 */
	public static String getYoutube() {
		return YOUTUBE;
	}
}
